package com.then.libary.parse;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.then.libary.bean.Article;
import com.then.libary.bean.Info;

public class ParseArticleJson {

	public static List<Info> getInfos(String json)
	{
		List<Info> infos = new ArrayList<Info>();
		try {
			JSONObject jonObject = new JSONObject(json);
			String statusString = jonObject.getString("status");
			if ("error".equals(statusString)) {
				
			}
			else if("success".equals(statusString)) {
				JSONArray jsonArray = new JSONArray(jonObject.getString("date"));
				for (int i = 0; i < jsonArray.length(); i++) {
					JSONObject jsonObject = jsonArray.getJSONObject(i);
					Info info = new Info();
					info.setId(jsonObject.getInt("id"));
					info.setText(jsonObject.getString("text"));
					info.setWho(jsonObject.getString("username"));
					info.setTime(jsonObject.getString("date"));
					info.setHead_img(jsonObject.getString("img_head"));
					infos.add(info);
				}
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return infos;
	}
	public Article getArticle(String json)
	{
		Article article = new Article();
		try {
			JSONObject jonObject = new JSONObject(json);
			String statusString = jonObject.getString("status");
			if ("error".equals(statusString)) {
				article=null;
			}
			else if("success".equals(statusString)) {
				jonObject = new JSONObject(jonObject.getString("date"));
				article.setId(jonObject.getString("id"));
				article.setHtmlString(jonObject.getString("text"));
				article.setTime(jonObject.getString("time"));
				article.setTitle(jonObject.getString("title"));
				article.setNowTime(jonObject.getString("nowTIme"));
				article.setImgString(jonObject.getString("smallImage"));
				article.setVote(jonObject.getString("vote"));
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return article;
		
	}
}
